/**
 * Class that handles the exceptions and contains all the functions needed by exception handling.
 * 
 * @author remo.lemma@lu.unisi.ch
 */

package ch.unisi.inf.projects.java.uom.exceptions;

// Libraries
import java.sql.SQLException;

public class ExceptionHandler {
	
	/** 
	 * Compares an SQLException with the given state and code
	 * @param se the SQLException
	 * @param state the state
	 * @param code the code
	 * @return true if the SQLException has the same code and state
	 */
	private static boolean compareSQLException(final SQLException se, final String state, final int code)
	{
		if (se == null)
			return false;
		return (se.getSQLState().equals(state) && se.getErrorCode() == code);
	}
	
	/**
	 * Checks if the generated exception means that the database doesn't exist
	 * @param se the raised exception
	 * @return true (the database does not exist) or false
	 */
	public static boolean dbNotFound (SQLException se)
	{
		return compareSQLException(se,UOMExceptions.DB_EXISTS.state(),UOMExceptions.DB_EXISTS.code());
	}
	
	/**
	 * Checks if the generated exception means that the database is shutting down correctly
	 * @param se the raised exception
	 * @return true (the database is shutting down correctly) or false
	 */
	public static boolean dbShutdownCorrectly (SQLException se)
	{
		return compareSQLException(se,UOMExceptions.DB_SHUTDOWN.state(),UOMExceptions.DB_SHUTDOWN.code());
	}

	/**
	 * Checks if the generated exception means that the table that we are trying to access doesn't exist
	 * @param se the raised exception
	 * @return true (the table doesn't exist) or false
	 */
	public static boolean tableNotExist (SQLException se)
	{
		return compareSQLException(se,UOMExceptions.TABLE_NOT_EXIST.state(),UOMExceptions.TABLE_NOT_EXIST.code());
	}
}
